/**
 * 音效播放，延后加载
 */
(function()
{
	var musicFun = function()
	{
		this.pack = '';
		// 记录时间长度ms
		this.src = {
			kk0 : [ 473 ],
			kk1 : [ 379 ],
			kk2 : [ 379 ],
			kk3 : [ 433 ],
			kk4 : [ 433 ],
			kk5 : [ 500 ],
			kk6 : [ 460 ],
			kk7 : [ 460 ],
			kk8 : [ 379 ],
			kk9 : [ 551 ],
			k10 : [ 541 ],
			k11 : [ 635 ],
			k12 : [ 582 ],
			k13 : [ 663 ],
			k14 : [ 663 ],
			k15 : [ 798 ],
			k16 : [ 717 ],
			k17 : [ 717 ],
			k18 : [ 717 ],
			k19 : [ 784 ],
			begin : [ 717, '开始' ],
			clear : [ 488, '' ], // 拟声词
			clearerror : [ 1691, '请先清除错误' ],
			clickgetorder : [ 1420, '请点击捞单' ],
			ding : [ 129, '' ], // 拟声词
			error : [ 1299, '出错啦' ],
			error0 : [ 1261, '' ], // 拟声词
			inputweight : [ 1366, '请输入重量' ],
			next : [ 892, '下一个' ],
			noordermore : [ 1663, '已经没有订单啦' ],
			numberlow : [ 1136, '数量不足' ],
			numberoverflow : [ 1339, '数量超出了' ],
			kok : [ 670, '' ], // 拟声词
			ordercome : [ 960, '订单来了' ],
			over : [ 839, '结束了' ],
			pick2 : [ 1176, '复核扫描' ],
			printfailed : [ 987, '打印失败' ],
			ready : [ 988, '准备好了' ],
			scanbarcode : [ 1352, '请扫描产品' ],
			scanexpress : [ 1785, '请扫描物流单号' ],
			scanlocation : [ 1406, '请扫描货位' ],
			scannewlocation : [ 1758, '请扫描新货位' ],
			scanserialnumber : [ 2000, '请扫描溯源码' ],
			startprint : [ 1163, '开始打印' ],
			takecare : [ 1163, '别开小差哟' ],
			takecare2 : [ 1744, '别急，慢慢来' ],
			welcome : [ 4125, '您好，欢迎使用智力库，我是小智' ],
			workwith : [ 3422, '很高兴和你一起工作，我是小智' ],
			prepick : [ 14911, '您好，欢迎使用拣货功能，请点击捞单或扫描货位，如果要停止工作，请取消已经捞出的货单，以免它锁定在您的用户名下' ],
			prepack : [ 4931, '您好，欢迎使用打包功能，请扫描物流单号' ],
			prereturn : [ 12434, '您好，欢迎使用归还产品功能，先扫描物流单号，再依次扫描货位号，并把产品归还至相应货位' ],
			prepick2 : [ 10062, '您好，欢迎使用复核扫描功能，请扫描物流单号，然后依次扫描包裹内所有产品' ],
			preaudit : [ 7113, '您好，欢迎使用称重扫描功能，请扫描快递号，并称重录入' ],
			inputsubexpressnumber : [ 3280, '请输入打印子母物流单的个数' ],
			inputexpressnumber : [ 2674, '请输入打印物流单的个数' ],
			scannextexpressno : [ 2536, '请扫描下一个物流单号' ],
			paperlows : [ 3280, '注意，纸张将要用完，请注意换纸' ],
			pickok : [ 1075, '拣货成功' ],
			pick2ok : [ 1075, '复核成功' ],
			billiscancel : [ 1323, '本单已取消' ],
			continuelastbill : [ 6000, '注意，上一单尚未结束，请向完成上一单' ],
			printallbasket : [ 6000, '已完成所有箱，点击提交开始打印物流单号' ],
			startnewbasket : [ 2500, '请扫描下一个快递单号' ],
			pleaseprintexpress : [ 1804, '请打印物流单号' ],
			scanreturnslabel : [ 1804, '请扫描包裹标签' ],
			pleasesubmitthisbill : [ 2397, '请提交并完成本单' ],
			scanallbasket : [ 2594, '已经扫描完所有的装箱产品' ],
			startnewbasket : [ 6202, '上一箱已经结束了，现在开始新的一箱，请扫描产品' ],
			scanbasketid : [ 2224, '请扫描箱标签号' ],
			scanwaveid : [ 2224, '请扫描波次号' ],
			pickover : [ 7264, '如果当前还有货单正在拣货时需要停止工作，请取消当前拣货单' ],
			havecancel : [ 10427, '订单已经被取消或拦截，请直接取消拣货，并把货品返回原处，销毁已经打印的物流单' ],
			cancelpickok : [ 6770, '取消拣货成功，请点击捞单或扫描货位，也可以停止工作' ],
			thisorderhavebasket : [ 3335, '注意，该单已经装箱部分产品' ],
			worksheetcome : [ 2850, '您有新的工单，请及时处理' ],
			newmailcome : [ 1400, '新消息来啦' ],
			scanallandreturn : [ 4967, '已扫描完所有货位，请点击提交完成归还' ],
			scanallandreturn : [ 4967, '已扫描完所有货位，请点击提交完成归还' ],
			scancontainerid : [ 1400, '请扫描容器号' ],
			overreturn : [ 1038, '归还成功' ],
			expressnamesf : [ 1038, '顺丰快递' ],
			expressnameyto : [ 963, '圆通快递' ],
			expressnameyd : [ 1013, '韵达快递' ],
			expressnamedbl : [ 1013, '德邦快递' ],
			expressnamezto : [ 989, '中通快递' ],
			expressnamesto : [ 1038, '申通快递' ],
			expressnameems : [ 1359, '邮政EMS' ],
			expressnameuc : [ 1013, '优速快递' ],
			expressnamejd : [ 964, '京东快递' ],
			expressname_plan_ : [ 7000, '预约物流，发货完成后，请到已完成订单里修改真实的快递单号' ],
		}
		var ns = app.getStorage('noSound');
		if (ns == 'yes')
			this.noSound = true;
		else if (ns == 'no')
			this.noSound = false;
		else
			this.noSound = !!config.noSound;
		this.playingNames = {};
		this.timeoutIndexList = [];
	};
	/**
	 * 本函数直接被app调用
	 */
	musicFun.prototype.play = function(name, delay, afterfun)
	{
		if (name && name.length > 16 && name.indexOf('http') >= 0) // 文件，直接播放
		{
			$('#thisMusic').attr('src', '');
			$('#thisMusic').attr('src', name);
			return;
		}
		if (!name || name == '_stop')
		{
			this.doStop();
			return;
		}
		name = (name + ' ').trim().toLowerCase();
		if (name.length <= 1)
			name = 'kk' + name;
		else if (name.length <= 2)
			name = 'k' + name;
		if (!this.src[name])
		{
			layer.msg('NO SOUND : ' + name);
			return;
		}
		var now = (new Date()).getTime();
		var that = this;
		// lastPlayOverTime 上一个语音结束时间
		if (!this.lastPlayOverTime || this.lastPlayOverTime < now)
			this.lastPlayOverTime = now;
		// 等待时间
		var waitTime = this.lastPlayOverTime - now;
		if (!this.src[name][0] || this.src[name][0] <= 0)
			this.src[name][0] = 500;
		this.lastPlayOverTime += this.src[name][0] + 200;
		if (delay > 0)
			this.lastPlayOverTime += delay;
		this.playingNames[name] = 1;
		if (waitTime > 0)
		{
			this.timeoutIndexList.push(setTimeout(function()
			{
				that.doPlay(name, delay, afterfun);
			}, waitTime));
			if (this.timeoutIndexList.length > 128)
				clearTimeout(this.timeoutIndexList.shift());
		}
		else
			this.doPlay(name, delay, afterfun);
		var re = '';
		var ki = waitTime > 0 ? -1 : 0;
		for ( var na in this.playingNames)
		{
			if (!this.src[na][1])
				this.src[na][1] = '';
			re += ' <i class="fa fa-volume-up size12" onclick="music.play(\'' + na + '\')"></i> ' + this.src[na][1];
		}
		return re;
	}
	/**
	 * 阅读数字
	 */
	musicFun.prototype.readNumber = function(number, delay, afterfun)
	{
		if (!number || number < 1)
			this.play('over', delay, afterfun);
		else if (number > 19)
			this.play('next', delay, afterfun);
		else
			this.play(number, delay, afterfun);
	}
	/**
	 * 播放出错的声音
	 */
	musicFun.prototype.error = function(afterfun)
	{
		var n = Math.floor(Math.random() * 2.9999) + 1;
		this.play();
		this.play('error0', 100);
		if (n != 2 && n != 3)
		{
			this.play('error', 1, afterfun);
		}
		else
		{
			this.play('error', 200);
			if (n == 2)
				this.play('takecare', 1, afterfun);
			else if (n == 3)
				this.play('takecare2', 1, afterfun);
		}
	}
	musicFun.prototype.setMusicPack = function(pack)
	{
		if (!pack)
			pack = '1';
		this.pack = pack;
		app.setStorage('musicPack', pack);
	}
	/**
	 * 播放所有声音
	 */
	musicFun.prototype.speakAll = function()
	{
		if (this.noSound)
		{
			errorMsg('please open sound at setting');
		}
		else
		{
			for ( var w in this.src)
			{
				this.play(w, 333);
			}
		}
	}
	/**
	 * 关闭
	 */
	musicFun.prototype.doStop = function()
	{
		this.lastPlayOverTime = 0;
		while (this.timeoutIndexList.length > 0)
		{
			clearTimeout(this.timeoutIndexList.shift());
		}
		this.timeoutIndexList = [];
		this.playingNames = {};
		if (typeof toAndroidApp == 'object' || typeof toAndroidApp == 'function')
		{
			toAndroidApp.play('_stop');
		}
		$('#thisMusic').attr('src', '');
	}
	/**
	 * 执行播放音乐，马上播放，播放完成后执行后续程序
	 */
	musicFun.prototype.doPlay = function(name, delay, afterfun)
	{
		if (!this.pack)
		{
			this.pack = app.getStorage('musicPack');
			if (!this.pack)
				this.pack = '1';
		}
		if (!this.src[name])
			return;
		var that = this;
		var mustOnWeb = !!this.src[name][2];
		if (!this.noSound)
		{
			var isPlay = false;
			if (!mustOnWeb && (typeof toAndroidApp == 'object' || typeof toAndroidApp == 'function'))
			{
				if (this.src[name] || name == '_stop')
				{
					if (toAndroidApp.play(name) == 'yes')
						isPlay = true;
				}
			}
			if (!isPlay && name != '_stop')
			{
				$('#thisMusic').attr('src', '');
				$('#thisMusic').attr('src', 'static/' + app.core + '/images/raw' + this.pack + '/' + name + '.mp3');
			}
		}
		// 执行后续程序
		if (afterfun)
		{
			if (!delay || delay <= 0)
				delay = 1;
			delay += this.src[name][0];
			this.timeoutIndexList.push(setTimeout(function()
			{
				afterfun();
			}, delay));
			if (this.timeoutIndexList.length > 128)
				clearTimeout(this.timeoutIndexList.shift());
		}
		this.timeoutIndexList.push(setTimeout(function()
		{
			if (that.playingNames[name])
				delete that.playingNames[name];
		}, 1));
	}
	window.music = new musicFun();
}());
if ($('#thisMusic').length <= 0)
{
	$('body').append('<audio id="thisMusic" src="" autoplay></audio>');
}
